config-cache
General purpose JavaScript object storage methods.
Install with npm
npm i config-cache --save
Usage
var Config = require('config-cache');
var config = new Config();
API
Initialize a new Cache
obj
{Object}: Optionally pass an object to initialize with.
var cache = new Cache();
Assign value
to key
or return the value of key
.
key
{String}value
{*}expand
{Boolean}: Resolve template strings with expanderreturns
{Object} Cache
: to enable chaining
cache.set(key, value);
Return the stored value of key
. If the value does not exist on the cache, you may pass true
as a second parameter to tell [set-object] to initialize the value as an empty object.
key
{*}create
{Boolean}returns
: {*}
cache.set('foo', 'bar');
cache.get('foo');
cache.set({data: {name: 'Jon'}})
cache.get('data', 'name');
Set a constant on the cache.
Example
cache.constant('site.title', 'Foo');
Return true if key
exists in cache
. Dot notation may be used for nested properties.
key
{String}returns
: {Boolean}
Example
cache.exists('author.name');
Return true if property
exists and has a non-null value. Dot notation may be used for nested properties.
property
{String}returns
: {Boolean}
Example
cache.has('author.name');
Add values to an array on the cache
. This method is chainable.
returns
{Object} Cache
: to enable chaining
Example
cache
.union('foo', ['b.hbs', 'c.hbs'], ['d.hbs']);
.union('foo', ['e.hbs', 'f.hbs']);
Extend the cache
with the given object. This method is chainable.
returns
{Object} Cache
: to enable chaining
Example
cache
.extend({foo: 'bar'}, {baz: 'quux'});
.extend({fez: 'bang'});
Or define the property to extend:
cache
.extend('a', {foo: 'bar'}, {baz: 'quux'})
.extend('b', {fez: 'bang'})
.extend('a.b.c', {fez: 'bang'});
Return the keys on this.cache
.
cache.keys();
Return true if key
is an own, enumerable property of this.cache
or the given obj
.
key
{String}obj
{Object}: Optionally pass an object to check.returns
: {Boolean}
cache.hasOwn([key]);
Clone the given obj
or cache
.
obj
{Object}: Optionally pass an object to clone.returns
: {Boolean}
cache.clone();
Return methods on this.cache
or the given obj
.
obj
{Object}returns
: {Array}
cache.methods('foo')
Methods for reading data files, processing template strings and
extending the cache.data
object.
Use expander to recursively expand template strings into their resolved values.
lookup
{*}: Any value to process, usually strings with a cache template, like <%= foo %>
or ${foo}
.opts
{*}: Options to pass to Lo-Dash _.template
.
Example
cache.process({a: '<%= b %>', b: 'c'});
Extend the cache.data
object with the given data. This method is chainable.
returns
{Object} Cache
: to enable chaining
Example
cache
.extendData({foo: 'bar'}, {baz: 'quux'});
.extendData({fez: 'bang'});
Extend the data
object with the value returned by plasma.
data
{Object|String|Array}: File path(s), glob pattern, or object of data.options
{Object}: Options to pass to plasma.
Example:
cache
.plasma({foo: 'bar'}, {baz: 'quux'});
.plasma({fez: 'bang'});
See the plasma documentation for all available options.
Extend the cache.data
object with data from a JSON or YAML file, or by passing an object directly - glob patterns or file paths may be used.
values
{Object|Array|String}: Values to pass to plasma.process
{Boolean}: If true
,returns
{Object} Cache
: to enable chaining
cache
.data({a: 'b'})
.data({c: 'd'});
console.log(config.cache);
When true
is passed as the last argumemnt data will
be processed by expander before extending cache.data
.
cache.data({a: '<%= b %>', b: 'z'})
Methods for clearing the cache, removing or reseting specific
values on the cache.
Omit properties from the cache
.
returns
{Object} Cache
: to enable chaining
Example:
cache
.omit('foo');
.omit('foo', 'bar');
.omit(['foo']);
.omit(['foo', 'bar']);
Remove key
from the cache, or if no value is specified the entire cache is reset.
Example:
cache.clear();
Usage Examples
.set
If expand: true
is defined on the options, the value will be set using expander.
Examples:
cache.set('a', {b: 'c'});
cache.set({a: {b: 'c'}});
cache
.set({a: {b: 'c'}})
.set('d', 'e');
Expand template strings with expander:
cache.set('a', {b: '${c}', c: 'd'}, true);
Visit the expander docs for more info.
Related
- option-cache: Simple API for managing options in JavaScript applications.
- map-cache: Basic cache object for storing key-value pairs.
- cache-base: Generic object cache for node.js/javascript projects.
- config-cache: General purpose JavaScript object storage methods.
- engine-cache: express.js inspired template-engine manager.
- loader-cache: Register loader functions that dynamically read, parse or otherwise transform file contents when the name of the loader matches a file extension. You can also compose loaders from other loaders.
- parser-cache: Cache and load parsers, similiar to consolidate.js engines.
- helper-cache: Easily register and get helper functions to be passed to any template engine or node.js application. Methods for both sync and async helpers.
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue
Running tests
Install dev dependencies:
npm i -d && npm test
Author
Jon Schlinkert
License
Copyright (c) 2015 Jon Schlinkert
Released under the MIT license
This file was generated by verb-cli on April 02, 2015.